<?php
namespace App\Http\Controllers\Admin;

use App\Models\BbsCategory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use App\Models\BbsQuestion;
class BbsCategoryController extends BaseController
{
    
    protected $category;
    /**
     * @see \App\Http\Controllers\Admin\BaseController::__construct()
     */
    public function __construct() {
        parent::__construct();
        $this->category = BbsCategory::where(array('parent_id'=>0,'type'=>'question'))->orderBy('created_at')->get();
    }
    
    /**
     * 分类列表
     */
    public function index()
    {
        if(!$this->user->can('viewBbsCategory')) abort(405);
        
        $category = BbsCategory::where(array('parent_id'=>0,'type'=>'question'))->orderBy('created_at')->get();
        
        return view('admin.bbs.category.list')->with('category',$category);
    }
    
    /**
     * 新增分类
     */
    public function create(Request $req)
    {
        if(!$this->user->can('editBbsCategory')) abort(405);
        
        $catid = $req->input('catid',0);
        
        return view('admin.bbs.category.add')
            ->with('category',$this->category)
            ->with('blademode','add')
            ->with('catid',$catid)
            ->with('cates',array());
    }
    
    /**
     * 编辑分类
     */
    public function edit($id)
    {
        if(!$this->user->can('viewBbsCategory')) abort(405);
        
        $cates = BbsCategory::where('id','=',$id)->first();
        
        return view('admin.bbs.category.add')->with('category',$this->category)->with('blademode','edit')->with('cates',$cates);
        
    }
    
    /**
     * 删除分类
     */
    public function destroy($id)
    {
        if(!$this->user->can('deleteBbsCategory')) 
            return $this->formatJson('error','抱歉，您没有相关操作权限');
        $category = BbsCategory::find($id);
        if(empty($category))
        {
            return $this->formatJson('error','资源不存在！');
        }
        $hasChild = BbsCategory::where('parent_id','=',$id)->get();
        $hasbbsq = BbsQuestion::where('category_id','=',$id)->get();
        if(count($hasChild)>0)
        {
            return $this->formatJson('error','该分类下存在子分类，请先删除子分类！');
        }
        if(count($hasbbsq)>0)
        {
            return $this->formatJson('error','该分类下存在帖子，请先删除帖子！');
        }
        if($category->delete())
            return  $this->formatJson('success','分类：［'.$category->title.'］ 删除成功');
    }
    
    /**
     * 分类保存
     */
    public function store(Request $req)
    {
        if(!$this->user->can('editBbsCategory')) abort(405);
        
        $data = $req->only('name','type','sort','catid','icon','is_hot');
        if($data['catid'] > 0)
        {
            $category = BbsCategory::find($data['catid']);            
        }
        else 
        {
            $category = new BbsCategory();
        }
        $category->title = $data['name'];
        $category->parent_id = $data['type'] ?:0;
        $category->sort = $data['sort'];
        $category->icon = $data['icon'];
        $category->type = 'question';
        $category->is_hot = $data['is_hot'];
        $category->save();
        return redirect("admin/bbs/category");
    }
    
    /**
     *  热门板块
     */
    public function hotCates(Request $request)
    {
        if(!$this->user->can('editBbsCategory')) abort(405);
        
        $id = $request->input('id');
        $is_hot = $request->input('is_disabled');
        $question = BbsCategory::find($id);
        
        $question->is_hot = $is_hot == 1 ? 0 : 1;
        if ($question->save())
            return  Response::json(array('status'=>'success','msg'=>'成功', 'data' => $question->is_hot));
    }
}